package demo2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Test {
    //花色
    private static final String[] SUITS = {"♥","♠","♣","♦"};

    public static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();

        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(SUITS[i],j);
                cards.add(card);
            }
        }
        return cards;
    }


    public static void shuffle(List<Card> cards) {
        Random random = new Random();
        for (int i = cards.size() - 1 ; i > 0 ; i--) {
            int j = random.nextInt(i);
            //交换
            /* int tmp = [i];
            [i] = [j];
            [j] = tmp;*/
            Card tmp = cards.get(i);
            cards.set(i,cards.get(j));
            cards.set(j,tmp);
        }
    }

    public static void main(String[] args) {
        List<Card> cards = buyCard();
        System.out.print(cards);
        System.out.println();

        System.out.println("洗牌");
        shuffle(cards);
        System.out.print(cards);
        System.out.println();

        //需求：3个人，轮流揭牌5张
        //1.每个人揭的牌放在哪？
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //2.每个人轮流揭牌5张
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //揭牌的动作
                Card card = cards.remove(0);
                //如何放到指定人的手里:用hand将每个人关联起来（二维数组）
                //hand获取到了j的下标，然后add一张牌（这张牌从全部的牌中删除，给到了j这个人手里）
                hand.get(j).add(card);
            }
        }
        System.out.println("第一个人的牌：");
        System.out.println(hand1);
        System.out.println("第二个人的牌：");
        System.out.println(hand2);
        System.out.println("第三个人的牌：");
        System.out.println(hand3);
        System.out.println("剩下的牌：");
        System.out.println(cards);
    }
}
